package com.exam.action;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.exam.dao.QuestionDao;
import com.exam.dao.QuestionDbDao;
import com.exam.model.QuestionForm;
import com.exam.service.PageService;
import com.exam.util.Tools;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class QuestionAction extends ActionSupport implements 
ModelDriven<QuestionForm> ,Preparable,RequestAware,ServletResponseAware {
	
	private PrintWriter out = null;
	private static final long serialVersionUID = -6620928865470434181L;

	private QuestionForm questionfm;
	private String dbid;  //题库id
	private String qid;   //试题id;
	private String pageNow="1"; //分页当前页数
	
	
	private static QuestionDao qdao=null;
	
	//筛选条件
	private String key="";
	private int status=-1;
	

	/**
	 * 添加试题
	 * @return
	 */
	public String save(){
		try {
			out = response.getWriter();
			if(qdao.addOneQuestion(questionfm)){
				out.print("success");
			}else{
				out.print("error");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(questionfm.toString());
		return null;
		//return "save-success";
	}
	
	public void prepareSave(){
		questionfm = new QuestionForm();
	}
	/**
	 * 试题列表
	 * @return
	 */
	public String list(){
		try {
			key = Tools.getUTF8Str(key);
			qdao =  new QuestionDao();
			this.request.put("OneQDB",new QuestionDbDao().getOneQDB(dbid));
			PageService page = new PageService(qdao.getCount(key,dbid,status),"question-list.action?dbid="+dbid+"&key="+key+"&status="+status+"&",pageNow);
			this.request.put("QuestionList", qdao.getQuestionList(key,dbid,status,page.getLimit()));
			this.request.put("pageInfo", page.getPageInfo());
			this.request.put("pageList", page.getPageList());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "list-success";
	}
	
	
	/**
	 * 编辑试题
	 * @return
	 */
	public String edit(){
		return "edit-success";
	}
	/**
	 * 获取回显数据
	 */
	public void prepareEdit(){
		try {
			questionfm =  new QuestionDao().getOneQuestion(qid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 更新试题信息
	 * @return
	 */
	public String update(){
		qdao = new QuestionDao();
		System.out.println(questionfm.toString());
		try {
			qdao.updateQuestion(questionfm);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "update-success";
	}
	public void prepareUpdate(){
		questionfm = new QuestionForm();
	}
	
	
	/**
	 * 删除试题
	 * @return
	 */
	public String delete(){
		qdao = new QuestionDao();
		qdao.deleteQuestion(qid);
		return "delete-success";
	}
	
	/**
	 * 更改试题状态
	 * @return
	 */
	public String changeStatus(){
		qdao = new QuestionDao();
		qdao.changeStatus(qid, status);
		return "status";
	}
	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
	}
	@Override
	public QuestionForm getModel() {
		return questionfm;
	}
	Map<String, Object> request = null;
	@Override
	public void setRequest(Map<String, Object> req) {
		this.request = req;
	}
	public String getPageNow() {
		return pageNow;
	}
	public void setPageNow(String pageNow) {
		this.pageNow = pageNow;
	}

	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}

	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public String getDbid() {
		return dbid;
	}
	public void setDbid(String dbid) {
		this.dbid = dbid;
	}
	public String getQid() {
		return qid;
	}

	public void setQid(String qid) {
		this.qid = qid;
	}

	
	private HttpServletResponse response;
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	
}
